package com.hbnu.system.utils;

import com.hbnu.system.config.BaseException;
import com.hbnu.system.constant.RoleConstant;
import com.hbnu.system.constant.StatusCheckConstant;
import com.hbnu.system.model.context.BaseContext;
import com.hbnu.system.model.entity.User;
import com.hbnu.system.service.IUserService;
import org.springframework.stereotype.Component;

/**
 * @author jxl
 */
@Component
public class CheckRoleUtil {

    private final IUserService iUserService;

    public CheckRoleUtil(IUserService iUserService) {
        this.iUserService = iUserService;
    }

    /**
     * 检查项目部分的权限
     * @param projectState 项目状态
     */
    public void checkPermissionProject(String projectState) {
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        
        // 普通用户（ROLE_USER）：只能在草稿状态删除/修改
        if (user.getRole().equals(RoleConstant.ROLE_USER)
                && !projectState.equals(StatusCheckConstant.DRAFT)
                && !projectState.equals(StatusCheckConstant.IN_PROGRESS_BACK)
                && !projectState.equals(StatusCheckConstant.PROJECT_FINISH_BACK)) {
            throw new BaseException("该项目目前状态不允许修改或删除!");
        }
        
        // 院级审核员（ROLE_ADMIN_COLLEGE）：可以在草稿、待院级审核、退回状态删除/修改
        // 禁止在待校级审核、项目在研、项目结题状态修改/删除
        if (user.getRole().equals(RoleConstant.ROLE_ADMIN_COLLEGE) && (
                projectState.equals(StatusCheckConstant.IN_PROGRESS_WAIT_SCHOOL_CHECK) ||
                projectState.equals(StatusCheckConstant.FINISH_WAIT_SCHOOL_CHECK) ||
                projectState.equals(StatusCheckConstant.IN_PROGRESS) ||
                projectState.equals(StatusCheckConstant.PROJECT_FINISH)
        )) {
            throw new BaseException("该项目目前状态不允许修改或删除!");
        }
        
        // 校级审核员（ROLE_ADMIN_SCHOOL）：可以在所有状态删除/修改，无需限制
    }

    /**
     * 检查权限
     * @param status 成果状态
     */
    public void checkPermissionAchievement(String status) {
        //普通用户待院级审核、待校级审核、审核通过状态下禁止修改
        String userId = BaseContext.getCurrentId();
        // 查询用户的权限
        User user = iUserService.getById(userId);
        if (user.getRole().equals(RoleConstant.ROLE_USER) &&
                (status.equals(StatusCheckConstant.WAIT_COLLEGE_CHECK) ||
                        status.equals(StatusCheckConstant.WAIT_SCHOOL_CHECK) ||
                        status.equals(StatusCheckConstant.CHECK_PASS))
        ) {
            throw new BaseException("该成果目前状态不允许修改或删除!");
        }
        //如果是院级审核员的话
        if (user.getRole().equals(RoleConstant.ROLE_ADMIN_COLLEGE) &&
                (status.equals(StatusCheckConstant.WAIT_SCHOOL_CHECK) ||
                        status.equals(StatusCheckConstant.CHECK_PASS))
        ) {
            throw new BaseException("该成果目前状态不允许修改或删除!");
        }
    }

    /**
     * 检查科研平台权限
     * @param tplatformState 科研平台状态
     */
    public void checkPermissionTplatform(String tplatformState) {
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        if (user.getRole().equals(RoleConstant.ROLE_USER) && (
                tplatformState.equals(StatusCheckConstant.WAIT_COLLEGE_CHECK) ||
                        tplatformState.equals(StatusCheckConstant.WAIT_SCHOOL_CHECK) ||
                        tplatformState.equals(StatusCheckConstant.CHECK_PASS)
        )) {
            throw new BaseException("该科研平台目前状态不允许修改或删除!");
        }
        //如果是院级审核员的话
        if (user.getRole().equals(RoleConstant.ROLE_ADMIN_COLLEGE) &&
                (tplatformState.equals(StatusCheckConstant.WAIT_SCHOOL_CHECK) ||
                        tplatformState.equals(StatusCheckConstant.CHECK_PASS))
        ) {
            throw new BaseException("该科研平台目前状态不允许修改或删除!");
        }
    }


}
